/**
 * 
 */
package com.rio.dandan.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.locks.ReentrantLock;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.rio.dandan.bean.PageBean;
import com.rio.dandan.dao.UserAccountDao;
import com.rio.dandan.dao.UserProfileDao;
import com.rio.dandan.exception.AccountExistsException;
import com.rio.dandan.pojo.UserAccount;
import com.rio.dandan.pojo.UserProfile;
import com.rio.dandan.service.UserAccountService;
import com.rio.dandan.util.IpUtils;
import com.rio.dandan.util.PasswordUtils;
import com.rio.dandan.util.PasswordUtils.Password;
import com.rio.dandan.util.SecurityUtils;
import com.rio.dandan.util.SessionUtils;
import com.rio.dandan.util.UUIDUtils;

/**
 * 
 * UserAccountServiceImpl.java
 * 
 * @author 宋陆
 * @version 1.0 2014-5-14
 */
@Service
public class UserAccountServiceImpl implements UserAccountService {

    private final ReentrantLock lock = new ReentrantLock();

    @Resource
    private UserAccountDao userAccountDao;

    @Resource
    private UserProfileDao userProfileDao;

    /**
     * @see com.rio.dandan.service.UserAccountService#findUserAccount(com.rio.dandan.bean.PageBean)
     */
    @Override
    public PageBean<UserAccount> findUserAccount(PageBean<UserAccount> pageBean) {
        userAccountDao.findUserAccount(pageBean);
        return pageBean;
    }

    /**
     * @see com.rio.dandan.service.UserAccountService#getUserAccount(java.lang.String)
     */
    @Override
    public UserAccount getUserAccount(String uid) {
        return userAccountDao.getUserAccount(uid);
    }

    /**
     * @see com.rio.dandan.service.UserAccountService#getUserAccountForUpdate(java.lang.String)
     */
    @Override
    public UserAccount getUserAccountForUpdate(String account) {
        return null;
    }

    /**
     * @see com.rio.dandan.service.UserAccountService#addUserAccount(com.rio.dandan.pojo.UserAccount)
     */
    @Override
    public UserAccount addUserAccount(UserAccount userAccount, UserProfile userProfile) {
        // 本地线程锁
        lock.lock();
        try {
            // 检测帐号是否已经存在
            if (userAccountDao.getUserAccountByAccount(userAccount.getUsername()) != null) {
                throw new AccountExistsException("ex.account_exists");
            }

            String uid = UUIDUtils.getUUID();
            userAccount.setUid(uid);// 设置唯一ID
            Password pwd = PasswordUtils.encryptPwd(userAccount.getPassword());
            userAccount.setPassword(pwd.getPwd());
            userAccount.setSalt(pwd.getSalt());// 密码种子
            userAccount.setGrade((short) 1); // 默认0级会员
            userAccount.setStatus((short) 1); // 默认正常使用
            userAccount.setIvtCode(UUID.randomUUID().toString()); // 邀请码
            userAccountDao.saveUserAccount(userAccount);

            userProfile.setUid(uid);
            userProfileDao.addUserProfile(userProfile);
        } finally {
            lock.unlock();
        }
        return userAccount;
    }

    /**
     * @see com.rio.dandan.service.UserAccountService#login(java.lang.String,
     *      java.lang.String)
     */
    @Override
    public UserAccount login(String username, String password) {
        SecurityUtils.login(username, password, true);
        String uid = SecurityUtils.getCurrUId();
        UserAccount userAccount = userAccountDao.getUserAccount(uid);
        userAccount.setLastIpLong(new Long(userAccount.getLoginIpLong()));
        userAccount.setLastIpStr(new String(userAccount.getLoginIpStr()));
        Calendar c = Calendar.getInstance();
        c.setTime(userAccount.getLoginTime());
        userAccount.setLastTime(c.getTime());
        String strIp = SessionUtils.getIpAddr();
        userAccount.setLoginIpLong(IpUtils.ipToLong(strIp));
        userAccount.setLoginTime(new Date());
        userAccount.setLoginIpStr(strIp);
        userAccountDao.updateUserAccount(userAccount);
        return userAccount;
    }

}
